package org.tote.service.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.tote.model.Role;

public class CustomUserDetails implements UserDetails {

	private static final long serialVersionUID = 1310744550362738456L;

	private Long id;
	private String name;
	private String username;
	private String password;
	private String clientIP;
	private String clientBrowser;
	private Date loggingTime = null;
	private boolean enabled;
	private boolean accountNonExpired;
	private boolean credentialsNonExpired;
	private boolean accountNonLocked;
	private List<GrantedAuthority> authorities;

	public CustomUserDetails(org.tote.model.User userEntity) {

		id = userEntity.getId();
		name = userEntity.getName();
		username = userEntity.getUsername();
		password = userEntity.getPassword();
		enabled = userEntity.getEnabled();
		accountNonExpired = userEntity.getEnabled();
		credentialsNonExpired = userEntity.getEnabled();
		accountNonLocked = userEntity.getEnabled();
		authorities = new ArrayList<GrantedAuthority>();
		for (Role r : userEntity.getRoles()) {
			authorities.add(new SimpleGrantedAuthority(r.getRole().toString()));
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public String getName() {
		return name;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	public String getClientIP() {
		return clientIP;
	}

	public void setClientIP(String clientIP) {
		this.clientIP = clientIP;
	}

	public String getClientBrowser() {
		return clientBrowser;
	}

	public void setClientBrowser(String clientBrowser) {
		this.clientBrowser = clientBrowser;
	}

	public Date getLoggingTime() {
		return loggingTime;
	}

	public void setLoggingTime(Date loggingTime) {
		this.loggingTime = loggingTime;
	}

	public Long getId() {
		return id;
	}

	@Override
	public String toString() {
		return "CustomUserDetails [id=" + id + ", name=" + name + ", username=" + username
				+ ", enabled=" + enabled + "]";
	}

}
